//
//  ClubListTips.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/9/23.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ClubListTipsVC.h"

@interface ClubListTipsVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ClubListTipsVC
{
    UITableView *_tableView;
    
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSArray *_responseArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    self.title = @"旅游Tips";
    
    [self requsetForTipsList];
    [self initTableViewWithTips];
    //去除多余的分割线
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)requsetForTipsList{
    //参数
    NSDictionary *parameters = @{
                                 @"clubId": self.clubId,
                                 @"pageNum":@"1",
                                 @"pageSize":@"10",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/club/tipsList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD showWithStatus:@"请求中"];
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //创建成功
            [SVProgressHUD showSuccessWithStatus:@"请求Tips列表成功"];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
- (void)initTableViewWithTips{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
#pragma - mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //
    NSString *_titleStr = _responseDic[@"list"][indexPath.row][@"title"];
    CGSize _titleStrSize = [BAAutoSizeWithWH BA_AutoSizeOfHeghtWithText:_titleStr font:[UIFont systemFontOfSize:17] width:(KScreenWidth - 40)];
    
    UILabel *titilLb = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, KScreenWidth - 40, _titleStrSize.height)];
    titilLb.font = [UIFont systemFontOfSize:17];
    titilLb.text = _titleStr;
    [cell.contentView addSubview:titilLb];
    
    NSString *_contentStr = _responseDic[@"list"][indexPath.row][@"content"];
    CGSize _contentStrSize = [BAAutoSizeWithWH BA_AutoSizeOfHeghtWithText:_contentStr font:[UIFont systemFontOfSize:13] width:(KScreenWidth - 40)];
    UILabel *contentLb = [[UILabel alloc]initWithFrame:CGRectMake(20, _titleStrSize.height + 15, KScreenWidth - 40, _contentStrSize.height)];
    contentLb.textColor = Gray_Color;
    contentLb.font = [UIFont systemFontOfSize:13];
    contentLb.numberOfLines = 0;
    contentLb.text = _contentStr;
    [cell.contentView addSubview:contentLb];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *_titleStr = _responseDic[@"list"][indexPath.row][@"title"];
    CGSize _titleStrSize = [BAAutoSizeWithWH BA_AutoSizeOfHeghtWithText:_titleStr font:[UIFont systemFontOfSize:17] width:(KScreenWidth - 40)];
    NSString *_contentStr = _responseDic[@"list"][indexPath.row][@"content"];
    CGSize _contentStrSize = [BAAutoSizeWithWH BA_AutoSizeOfHeghtWithText:_contentStr font:[UIFont systemFontOfSize:13] width:(KScreenWidth - 40)];
    return 30 + _titleStrSize.height + _contentStrSize.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
